package org.flown.shop.dto;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.Getter;
import org.flown.shop.DirectoryInformation;
import org.flown.shop.entity.Goods;
import org.flown.shop.entity.error.ReadError;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


@Getter
public class GoodsDTO
{
    private final int id;
    private final int sellerID;
    private final int storeID;
    private final String name;
    private final float price;
    private final String detail;//用时转json


    public GoodsDTO(Goods goods)
    {
        this.id = goods.getID();
        this.sellerID = goods.getSellerID();
        this.storeID = goods.getStoreID();
        this.name = goods.getName();
        this.price = goods.getPrice();
        this.detail = goods.getDetail();
    }


    public final String getDirectoryRealPath() {return DirectoryInformation.GOODS.getRealPath() + "/" + this.storeID + "/" + this.id;}
    public final String getCoverFileRealPath() {return this.getDirectoryRealPath() + "/cover.jpg";}
    public final String getDetailFileRealPath() {return this.getDirectoryRealPath() + "/detail.json";}
    public final String getDetailDirectoryRealPath() {return this.getDirectoryRealPath() + "/detail";}


    public final String getCommentFileRealPath() {return this.getDirectoryRealPath() + "/comment.json";}
    public final String getCommentDirectoryRealPath() {return this.getDirectoryRealPath() + "/comment";}


    public String getPictureRealPath(String name, String format) {return this.getDetailDirectoryRealPath() + "/" + name + "." + format;}


    /**
     * @param index 从1开始
     * */
    public String getPictureFormat(int index)
            throws JsonProcessingException, ReadError
    {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode root = (ArrayNode)mapper.readTree(this.detail);
        JsonNode node = root.get(index);
        if (!node.isObject())
            throw new ReadError("error index");
        return node.get("format").toString();
    }
    public String getPictureFormatByName(String name)
            throws IOException
    {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode root = (ArrayNode)mapper.readTree(this.detail);
        int count = root.size() - 1;
        for(int i = 0; i < count; i++)
        {
            JsonNode node = root.get(i + 1);
            if (!node.isObject())//不是对象，则不是图片
                continue;
            if (node.get("name").toString().equals(name))
            {
                return node.get("format").toString();
            }
        }
        return null;
    }




    public byte[] getCoverPicture()
            throws IOException
    {
        File file = new File(this.getCoverFileRealPath());
        FileInputStream input = new FileInputStream(file);
        return input.readAllBytes();
    }
    /**
     * @param index 从1开始
     * */
    public byte[] getPicture(int index)
            throws IOException, ReadError
    {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode root = (ArrayNode)mapper.readTree(this.detail);
        int count = root.size() - 1;
        if (index == 0 || index > count)
            throw new ReadError("object not exists");
        JsonNode node = root.get(index);
        if (!node.isObject())//不是对象，则不是图片
            throw new ReadError("isn't object");
        File file = new File(this.getPictureRealPath(node.get("name").toString(), node.get("format").toString()));
        FileInputStream input = new FileInputStream(file);
        return input.readAllBytes();
    }
    public byte[] getPictureByName(String name)
            throws IOException
    {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode root = (ArrayNode)mapper.readTree(this.detail);
        int count = root.size() - 1;
        for(int i = 0; i < count; i++)
        {
            JsonNode node = root.get(i + 1);
            if (!node.isObject())//不是对象，则不是图片
                continue;
            if (node.get("name").toString().equals(name))
            {
                File file = new File(this.getPictureRealPath(node.get("name").toString(), node.get("format").toString()));
                FileInputStream input = new FileInputStream(file);
                return input.readAllBytes();
            }
        }
        return null;
    }
    public byte[][] getPictures()
            throws IOException, ReadError
    {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode root = (ArrayNode)mapper.readTree(this.detail);
        int count = root.size() - 1;
        byte[][] bytes = new byte[count][];
        for (int i = 0; i < count; i++)
        {
            JsonNode node = root.get(i + 1);
            if (node == null)
                throw new ReadError("node == null");
            if (!node.isObject())//不是对象则下一个
                continue;
            File file = new File(this.getPictureRealPath(node.get("name").toString(), node.get("format").toString()));
            if (!file.exists())
                throw new ReadError("file isn't exists");
            FileInputStream input = new FileInputStream(file);
            bytes[i] = input.readAllBytes();
        }
        return bytes;
    }
}

